const { calcLimit } = require("../modal/sqlLimit.config");

class housesService {
  constructor(app) {
    this.app = app;
  }
  async findById(id) {
    return await this.app.mysql.getOne("houses", { id });
  }
  async findAll() {
    return await this.app.mysql.findAll("houses");
  }

  async getList(data) {
    return await this.app.mysql.select("houses", {
      where: data.where || {},
      like: data.like || {}, // like 条件
      orders: [{ field: "id", direction: "desc" }], // 排序方式
      offset: data.page
        ? calcLimit(Number(data.page), Number(data.pageSize)).page
        : null, // 数据偏移量
      limit: data.page
        ? calcLimit(Number(data.page), Number(data.pageSize)).pageSize
        : null, // 返回数据量
    });
  }

  async getCount(data) {
    return await this.app.mysql.count("houses", {
      where: data.where || {},
      like: data.like || {}, // like 条件
    });
  }

  async find(data) {
    return await this.app.mysql.getOne("houses", { ...data });
  }
  async create(data) {
    return await this.app.mysql.insert("houses", { ...data });
  }
  async update(data, condition) {
    return await this.app.mysql.update("houses", data, condition);
  }
  async delete(id) {
    return await this.app.mysql.delete("houses", { id });
  }
}

module.exports = housesService;
